package domain;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;



public class Display {

	private Activity myAct = new Activity();
	private Task myTask = new Task();
	private Resource myRes = new Resource();
	private Person myPer = new Person();
	private Material myMat = new Material();
	private ArrayList<Activity> activityList = new ArrayList<Activity>();
	private ArrayList<Task> taskList = new ArrayList<Task>();
	private ArrayList<Person> personList = new ArrayList<Person>();
	private ArrayList<Material> materialList = new ArrayList<Material>();
	
	
	public Display() {
		super();
	}


	public Activity getMyAct() {
		return myAct;
	}



	public void setMyAct(Activity myAct) {
		this.myAct = myAct;
	}



	public Task getMyTask() {
		return myTask;
	}



	public void setMyTask(Task myTask) {
		this.myTask = myTask;
	}



	public Resource getMyRes() {
		return myRes;
	}



	public void setMyRes(Resource myRes) {
		this.myRes = myRes;
	}

	/**
	 * This method creates the csv file
	 * @param myProject
	 */
	public void generateCsvFile(Project myProject)
	{
		activityList = myAct.selectbyProject(myProject);
		
		try
		{
		    FileWriter writer = new FileWriter("c:\\displayproject.csv");
	 
		    writer.append("Project Details");
		    writer.append('\n');
		    
		    writer.append("id");
		    writer.append(',');
		    writer.append("name");
		    writer.append(',');
		    writer.append("description");
		    writer.append(',');
		    writer.append("start date");
		    writer.append('\n');
		    
		    writer.append(Integer.toString(myProject.getId()));
		    writer.append(',');
		    writer.append(myProject.getName());
		    writer.append(',');
		    writer.append(myProject.getDesc());
		    writer.append(',');
		    writer.append(myProject.getDateToString());
		    writer.append('\n');
		    writer.append('\n');
	 
		    
		    
		    for(Activity act : activityList)
		    {
		    	writer.append("Activity Details");
			    writer.append('\n');
			    
			    writer.append("id");
		    	writer.append(',');
		    	writer.append("name");
		    	writer.append(',');
		    	writer.append("description");
		    	writer.append(',');
		    	writer.append("start date");
		    	writer.append(',');
		    	writer.append("hours");
		    	writer.append(',');
		    	writer.append("deliverable");
		    	writer.append('\n');
		    	
		    	writer.append(Integer.toString(act.getId()));
		    	writer.append(',');
		    	writer.append(act.getName());
		    	writer.append(',');
		    	writer.append(act.getDesc());
		    	writer.append(',');
		    	writer.append(act.getDateToString());
		    	writer.append(',');
		    	writer.append(Integer.toString(act.getHours()));
		    	writer.append(',');
		    	writer.append(act.getDeliverable());
		    	writer.append('\n');
		    	
		    	taskList = myTask.selectbyActivity(act);
		    	
		    	writer.append('\n');
			    
			    
		    	
		    	 for(Task task : taskList)
				 {
		    		 	writer.append("Task Details");
					    writer.append('\n');
					    
					    writer.append("id");
				    	writer.append(',');
				    	writer.append("name");
				    	writer.append(',');
				    	writer.append("description");
				    	writer.append(',');
				    	writer.append("start date");
				    	writer.append(',');
				    	writer.append("hours");
				    	writer.append(',');
				    	writer.append("deliverable");
				    	writer.append('\n');
		    		 
				    	writer.append(Integer.toString(task.getId()));
				    	writer.append(',');
				    	writer.append(task.getName());
				    	writer.append(',');
				    	writer.append(task.getDesc());
				    	writer.append(',');
				    	writer.append(task.getDateToString());
				    	writer.append(',');
				    	writer.append(Integer.toString(task.getHours()));
				    	writer.append(',');
				    	writer.append(task.getDeliverable());
				    	writer.append('\n');
				    	
				    	writer.append('\n');
				    	
				    	myRes = myRes.selectbyTask(task);
				    	
				    	writer.append("Resource Details");
				    	writer.append('\n');
				    	
				    	writer.append("id");
				    	writer.append(',');
				    	writer.append("name");
				    	writer.append(',');
				    	writer.append("start date");
				    	writer.append(',');
				    	writer.append("hours");
				    	writer.append('\n');
				    	
				    	writer.append(Integer.toString(myRes.getResourceId()));
				    	writer.append(',');
				    	writer.append(myRes.getName());
				    	writer.append(',');
				    	writer.append(myRes.getDateToString());
				    	writer.append(',');
				    	writer.append(Integer.toString(myRes.getHours()));
				    	writer.append('\n');
				    	
				    	writer.append('\n');
				    	
				    	personList = myPer.selectbyResource(myRes);
				    	
				    	if(personList != null)
				    	{
				    	
					    	writer.append("Person Details");
				    		writer.append('\n');
				    		
				    		writer.append("id");
				    		writer.append(',');
				    		writer.append("name");
				    		writer.append(',');
				    		writer.append("phone");
				    		writer.append(',');
				    		writer.append("address");
				    		writer.append(',');
				    		writer.append("salary");
				    		writer.append('\n');
				    		
					    	for(Person per : personList)
					    	{
					    		writer.append(Integer.toString(per.getId()));
					    		writer.append(',');
					    		writer.append(per.getName());
					    		writer.append(',');
					    		writer.append(per.getPhone());
					    		writer.append(',');
					    		writer.append(per.getAddress());
					    		writer.append(',');
					    		writer.append(Integer.toString(per.getSalary()));
					    		writer.append('\n');
					    		
					    	}
				    	}
				    	writer.append('\n');
				    	
				    	materialList = myMat.selectbyResource(myRes);
				    	
				    	if(materialList != null)
				    	{
					    	writer.append("Material Details");
					    	writer.append('\n');
					    	
					    	writer.append("id");
					    	writer.append(',');
					    	writer.append("name");
					    	writer.append('\n');
					    	
					    	for(Material mat : materialList)
					    	{
					    		writer.append(Integer.toString(mat.getId()));
						    	writer.append(',');
						    	writer.append(mat.getName());
						    	writer.append('\n');
					    	}
				    	}
				    }
		    	 
		    	 writer.append('\n');
		    }
		    
		 
	 
		    writer.flush();
		    writer.close();
		}
		catch(IOException e)
		{
		     e.printStackTrace();
		} 
	}
	
	/**
	 * This method prints the information in csv format on console
	 * @param myProject
	 */
	public void printCsv(Project myProject)
	{
		activityList = myAct.selectbyProject(myProject);
		
		System.out.println("Project Details");
		System.out.println("id,name,description,start date");
	    System.out.println(myProject.getId()+","+myProject.getName()+","+myProject.getDesc()+","+myProject.getDateToString());
	    
	    System.out.println();
	    
	    
	    for(Activity act : activityList)
	    {
	    	System.out.println("Activity Details");
		    System.out.println("id,name,description,start date,hours,deliverable");
	    	System.out.println(act.getId()+","+act.getName()+","+act.getDesc()+","+act.getDateToString()+","+act.getHours()+","+act.getDeliverable());
	
	    	System.out.println();
	    	
	    	
	    
		    taskList = myTask.selectbyActivity(act);
		    
		    for(Task t : taskList)
		    {
		    	System.out.println("Task Details");
			    System.out.println("id,name,description,start date,hours,deliverable");
		    	System.out.println(t.getId()+","+t.getName()+","+t.getDesc()+","+t.getDateToString()+","+t.getHours()+","+t.getDeliverable());
		    	System.out.println();
		    	
		    	myRes = myRes.selectbyTask(t);
		    	
		    	System.out.println("Resource Details");
		    	System.out.println("id,name,start date,hours");
		    	System.out.println(myRes.getResourceId()+","+myRes.getName()+","+myRes.getDateToString()+","+myRes.getHours());
		    	
		    	System.out.println();
		    	
		    	personList = myPer.selectbyResource(myRes);
		    	
		    	if(personList != null)
		    	{
		    		System.out.println("Person Details");
		    		System.out.println("id,name,phone,address,salary");
		    		
		    		for(Person per : personList)
		    		{
		    			System.out.println(per.getId()+","+per.getName()+","+per.getPhone()+","+per.getAddress()+","+per.getSalary());
		    		}
		    		
		    		System.out.println();
		    	}
		    	
		    	materialList = myMat.selectbyResource(myRes);
		    	
		    	if(materialList != null)
		    	{
		    		System.out.println("Material Details");
		    		System.out.println("id,name");
		    		
		    		for(Material mat : materialList)
		    		{
		    			System.out.println(mat.getId()+","+mat.getName());
		    		}
		    	}
		    }
		    
		    System.out.println();
	    } 
	}
	
	
}
